package com.Webserver.controller;

import com.Webserver.http.HttpRequest;
import com.Webserver.http.HttpResponse;
import com.Webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class UserController {
    private static String userDirName = "./users/";

    static {
        File userDir = new File(userDirName);
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }

    public void reg(HttpRequest request, HttpResponse response) {

        System.out.println("开始处理请求用户注册...");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");

        if (username == null || password == null || nickname == null || ageStr == null || ageStr.matches("[1-9]+")) {
            response.setEntity(new File("./webapps/myweb/reg_error.html"));
            return;
        }

        File file = new File(userDirName + username + ".obj");
        if (file.exists()) {
            response.setEntity(new File("./webapps/myweb/reg_have_user.html"));
            return;
        }

        int age = Integer.parseInt(ageStr);
        User user = new User(username, password, nickname, age);

        try (
                ObjectOutputStream ots = new ObjectOutputStream(
                        new FileOutputStream(userDirName + username + ".obj")
                );
        ) {
            ots.writeObject(user);
            response.setEntity(new File("./webapps/myweb/reg_success.html"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("用户注册请求处理完毕!");
    }

    public void login(HttpRequest request, HttpResponse response) {

        System.out.println("等待用户登录...");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username == null || password == null) {
            response.setEntity(new File("./webapps/myweb/login_error.html"));
            return;
        }

        File file = new File(userDirName + username + ".obj");
        if (file.exists()) {
            try (
                    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            ) {
                User user = (User) ois.readObject();
                String code = user.getPassword();
                if (password.equals(code)) {
                    response.setEntity(new File("./webapps/myweb/login_success.html"));
                    return;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        response.setEntity(new File("./webapps/myweb/login_fail.html"));
        System.out.println("登录处理完毕!!");
    }

    public void showAllUser(HttpRequest request, HttpResponse response) {
        System.out.println("页面生成...");
        List<User> userList = new ArrayList<>();
        File userDir = new File(userDirName);

        File[] subs = userDir.listFiles(e -> e.getName().endsWith(".obj"));
        for (File userFile : subs) {
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(userFile));
            ) {
                User user = (User) ois.readObject();
                userList.add(user);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        userList.forEach(System.out::println);


        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>用户列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>用户列表</h1>");
        pw.println("<table border=\"2\">");
        pw.println("<tr>");
        pw.println("<td>用户名</td>");
        pw.println("<td>密码</td>");
        pw.println("<td>昵称</td>");
        pw.println("<td>年龄</td>");
        pw.println("</tr>");
        for (User user : userList) {
            pw.println("<tr>");
            pw.println("<td>" + user.getUsername() + "</td>");
            pw.println("<td>" + user.getPassword() + "</td>");
            pw.println("<td>" + user.getNickname() + "</td>");
            pw.println("<td>" + user.getAge() + "</td>");
            pw.println("</tr>");
        }
        pw.println(" </table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        response.setContentType("text/html");
        System.out.println("页面生成处理完毕!");
    }
}
